package com.mes.fixture.server.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.fixture.mapper.FixtureMapper;
import com.mes.fixture.mapper.FixtureTypeMapper;
import com.mes.fixture.server.FixtureServer;
import com.mes.pojo.device.Device;
import com.mes.pojo.fixture.Fixture;
import com.mes.pojo.fixture.FixtureType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/23 9:58
 */
@Service
public class FixtureServerImpl implements FixtureServer {
    @Autowired
    private FixtureMapper fixtureMapper;
    @Autowired
    private FixtureTypeMapper fixtureTypeMapper;

    @Override
    public MESResult queryAll(Fixture fixture, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<Fixture> fixtures = fixtureMapper.selectAll(fixture);
        for (Fixture fixtureSon : fixtures) {
            FixtureType fixtureType = fixtureTypeMapper.selectByFixtureTypeId(fixtureSon.getFixtureTypeId());
            fixtureSon.setFixtureType(fixtureType);
        }
        PageInfo<Fixture> fixturePageInfo = new PageInfo<>(fixtures);
        return MESResult.success(fixturePageInfo);
    }

    @Override
    public MESResult queryById(String fixtureId) {
        return MESResult.success(fixtureMapper.selectById(fixtureId));
    }

    @Override
    public MESResult queryByTypeId(String fixtureTypeId) {
        return MESResult.success(fixtureMapper.selectByFixtureTypeId(fixtureTypeId));
    }

    @Override
    public MESResult delete(String fixtureId) {
        Fixture fixture = fixtureMapper.selectById(fixtureId);
        fixture.setFixtureStatus(99);
        return MESResult.success(fixtureMapper.updateFixture(fixture));
    }

    @Override
    public MESResult update(Fixture fixture) {
        Fixture same = fixtureMapper.selectByName(fixture.getFixtureName());
        if (same != null && !same.getFixtureId().equals(fixture.getFixtureId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        return MESResult.success(fixtureMapper.updateFixture(fixture));
    }

    @Override
    public MESResult insert(Fixture fixture) {
        Fixture same = fixtureMapper.selectByName(fixture.getFixtureName());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        String maxId = fixtureMapper.selectMaxId();
        String[] split = maxId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10){
            strNum = "0000" + num;
        }else if (num < 100){
            strNum = "000" + num;
        }else if (num < 1000){
            strNum = "00" + num;
        }else if (num < 10000){
            strNum = "0" + num;
        }else {
            strNum = num + "";
        }
        fixture.setFixtureId(split[0] + "_" + strNum);
        return MESResult.success(fixtureMapper.insertFixture(fixture));
    }

    @Override
    public MESResult getExport(Fixture fixture, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<Fixture> fixtures = fixtureMapper.selectAll(fixture);
        List<Fixture> fixtureList = new PageInfo<>(fixtures).getList();
        //判断文件夹是否存在不存在便创建文件夹
        File folder = new File("D:\\MES\\Excel");
        if (!folder.exists()){
            folder.mkdirs();
        }
        EasyExcel.write("D:\\MES\\Excel\\fixture.xlsx",Fixture.class).sheet("工装夹具数据-"+pageNum).doWrite(fixtureList);
        return MESResult.success();
    }
}
